#include "myscene.h"
#include<QPainter>
#include<QGraphicsSceneDragDropEvent>
MyScene::MyScene()
{

}

void MyScene::drawBackground(QPainter *painter, const QRectF &rect)
{
    if(!isShowGrid)
            return;

        Q_UNUSED(rect);
        QPen pen;

        qreal left=rect.left();
        qreal right=rect.right();
        qreal top=rect.top();
        qreal bottom=rect.bottom();

        //边界值调整
        left = (left/128)*128;
        right = (right/128)*128;
        top = (top/128)*128;
        bottom = (bottom/128)*128;

        pen.setColor(QColor(0,0,0));
        pen.setWidth(2);
        pen.setStyle(Qt::SolidLine);
        painter->setPen(pen);
        //绘制坐标轴
        painter->drawLine(left,0,right,0);
        painter->drawLine(0,top,0,bottom);

        pen.setColor(QColor(60,60,60));
        pen.setWidth(0);
        pen.setStyle(Qt::DashLine);
        painter->setPen(pen);
        //绘制横线
        //绘制Y轴正半轴
        for(int i=0;i>=top;i-=128)
        {
            painter->drawLine(left,i,right,i);
        }
        //绘制Y轴负半轴
        for(int i=0;i<=bottom;i+=128)
        {
            painter->drawLine(left,i,right,i);
        }

        //绘制竖线
        //绘制X轴正半轴
        for(int i=0;i<=right;i+=128)
        {
            painter->drawLine(i,top,i,bottom);
        }
        //绘制X轴负半轴
        for(int i=0;i>=left;i-=128)
        {
            painter->drawLine(i,top,i,bottom);
        }

}

void MyScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{
    event->accept();
}
